<?php

/*******************************************************************************
 * 
 * KSE BASIC
 * 
 * 
 * Template class which have default methods
 * such as logging
 * Log class, drops messages to chrome, firebug consoles
 * Includes, requires
 * XML parser
 * print_pre
 * 
 ******************************************************************************/

abstract class kseBasic{
    
    
    /***************************************************************************
     * 
     * FILE INCLUDING FUNCTIONS
     * 
     **************************************************************************/
    //include function analogue
    public function kseInclude($file, $message = false){
        
        if(file_exists($file)){
            include($file);
            if($message){
                self::info($message ."........SUCCESED");
            }
            return TRUE;
        }
	else{
            self::error("Error can't include file ". $file);
            if($message){
                self::warn($message ."........FAILED");
            }
            return FALSE;
        }
        
    }
    
    //require function analog
    public function kseRequire($file, $message = false){
        if(file_exists($file)){
            include($file);
            if($message){
                self::info($message ."........SUCCESED");
            }
        }
	else{
            self::error("Error can't include file ". $file);
            if($message){
                self::warn($message ."........FAILED");
            }
            Core::get()->kseDie();
        }
        
    }
    /***************************************************************************
     * 
     * END OF FILE INCLUDING FUNCTIONS
     * 
     =========================================================================*/
    
    
    
    
    
    
    /***************************************************************************
     * 
     * CONSOLE LOGGER
     * 
     * Logging methods: warning, info, debug, error
     * 
     * --------------------------------------------
     * Logging Levels
     * 0: no log
     * 1: info
     * 2: info & warn
     * 3: info & warn & error
     * 4: full log
     **************************************************************************/
    
    public $propertyList = array("LOG_LEVEL" => 0);
    
    public function warn($message){
        if(Core::get()->propertyList["LOG_LEVEL"] > 1){
            Log::addLog("console.warn(\"". get_called_class() .":: ". addslashes($message) ."\"); ");
        }
    }
    
    public function error($message){
        if(Core::get()->propertyList["LOG_LEVEL"] > 2){
            Log::addLog("console.error(\"". get_called_class() .":: ". addslashes($message) ."\"); ");
        }
    }
    
    public function debug($message){
        if(Core::get()->propertyList["LOG_LEVEL"] > 3){
            Log::addLog("console.debug(\"". get_called_class() .":: ". addslashes($message) ."\"); ");
        }
    }
    
    public function info($message){
        if(Core::get()->propertyList["LOG_LEVEL"] > 0){
            Log::addLog("console.info(\"". get_called_class() .":: ". addslashes($message) ."\"); ");
        }
    }
    /***************************************************************************
     * 
     * END OF CONSOLE LOGGER
     * 
     =========================================================================*/
    
    
    
    
    //@todo optimize hierarchy
    /***************************************************************************
     * 
     * XML PARSER
     * 
     * parse xml from file or string
     * in object structure
     * EXAMPLE:
     * <key>value</key>
     * $this->key = value
     * 
     **************************************************************************/
    var $parser;
    var $parser_module = false;
    //parse from string
    public function parseString($data){
        $this->parser = xml_parser_create();

        xml_set_object($this->parser, $this);
        xml_set_element_handler($this->parser, "tag_open", "tag_close");
        xml_set_character_data_handler($this->parser, "cdata");
        
        xml_parse($this->parser, $data);
    }
    
    //parse from file
    public function parseFile($file){
        $handle = fopen($file, "rb");
        $contents = fread($handle, filesize($file));
        fclose($handle);
        $this->parseString($contents);
    }

    public function tag_open($parser, $tag, $attributes){
        if($tag != "MODULES"){
            $this->current_property = $tag;
            $this->debug("Added property ". $this->current_property ." from tag ". $tag);
        }
        else{
            $this->parser_module = True;
            $this->debug("Found Module Open tag");
        }
    }

    public function cdata($parser, $cdata){
        $cdata = trim($cdata);
        if($cdata!=""){
            if(!$this->parser_module){
                $this->propertyList[$this->current_property] = $cdata;
                $this->debug("Added value ". $cdata ." to property ". $this->current_property);
            }
            else{
                $this->nsModules[] = $cdata;
                $this->debug("Added ". $cdata ." to nsModules");
            }
        }
    }

    public function tag_close($parser, $tag){
        if($tag == "MODULES"){
            $this->parser_module = false;
            $this->debug("Found Module Close tag");
        }    
    }

    /***************************************************************************
     * 
     * END OF XML PARSER
     * 
     =========================================================================*/
    
    
    
    /***************************************************************************
     * 
     * OUTPUT FUNCTIONS
     * 
     **************************************************************************/
    //print with pre
    public function print_pre($subject){
        echo "<pre>";
        print_r($subject);
        echo "</pre>";
    }
    /***************************************************************************
     * 
     * END OF OUTPUT FUNCTIONS
     * 
     =========================================================================*/
 
}

/*******************************************************************************
 ===============================================================================
 * 
 * END OF KSE BASIC
 * 
 =============================================================================
 =============================================================================*/







/*******************************************************************************
 * 
 * KSE CONTROLLER
 * 
 * 
 * Abstaract class for all controller
 * 
 ******************************************************************************/
abstract class kseController extends kseBasic{
    //controller name 
    public $name;
    public $model;
    public $view;

    public $framework = FALSE;
    public $namespace = array();
    
    public $propetyList = array();
    
    abstract public function getNamespaces();
    
    abstract public function showSysPage();
    
    abstract public function makeResult($objects);
    
    public function __construct(){
        
        self::info("Initialized");
        //$this->parseFile("")
        $this->getNamespaces();
        $this->transferNamespaces();
    }
    
    
    
    public function transferNamespaces(){
        
        self::info("Transfering url namespaces to Core");
        Core::get()->nsURLs[$this->name] = $this->namespace;
        
    }
    
    public function getContentFromURL($url, $required = TRUE){

        ob_start();
        ob_implicit_flush(0);
        if($required){
            $this->kseRequire($url, "Including url - ". $url);
        }
        else{
            $this->kseInclude($url, "Including url - ". $url);
        }
        $content = ob_get_contents();
        ob_end_clean();
        return $content;

    }
}

class Objects extends kseBasic{
    private $name;
    private $owner;
    private $content;
    private $tag;
    
    public function __construct($name, $owner, $tag) {
        
        $this->setName($name);
        $this->setOwner($owner);
        $this->setTag($tag);
        self::info($this->name ."::". $owner ." created");
        
    }


    /*
     * SETTERS
     */
    public function setName($param) {
        $this->name = $param;
    }
    
    public function setOwner($param) {
        $this->owner = $param;
    }
    
    public function setContent($param) {
        $this->content = $param;
    }
    
    public function setTag($param) {
        $this->tag = $param;
    }
    
    /*
     * GETTERS
     */
    
    public function getName(){
        return $this->name;
    }
    
    public function getOwner(){
        return $this->owner;
    }
    
    public function getContent(){
        return $this->content;
    }
    
    public function getTag(){
        return $this->tag;
    }
}

class Log{
    public static $logList = array();
    
    public function addLog($log){
        self::$logList[] = $log;
    }
    
    public function outputLog(){
        echo "<script>";
        foreach (self::$logList as $value) {
            echo trim($value);
        }
        echo "</script>";
    }
}

?>